package com.jinanzhuan.sqlciphertest;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class GcmTestActivity extends AppCompatActivity {
    private static final String TAG = GcmTestActivity.class.getSimpleName();
    private EditText contentText;
    private SharedPreferences preferences;
    private EncryptUtils encryptUtils;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, GcmTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gcm_test);

        contentText = findViewById(R.id.et_input);

        initSP();

        initGCM();
    }

    private void initSP() {
        preferences = getSharedPreferences("encrypt", Context.MODE_PRIVATE);
    }

    public void saveData(View view) {
        String content = contentText.getText().toString().trim();
        Log.e("TAG", "want to save content: "+content);
        String aesEncrypt = encryptUtils.aesEncrypt(content, 1);
        Log.e("TAG", "encrypt content: "+aesEncrypt);
        preferences.edit().putString("password", aesEncrypt).commit();

    }

    public void getData(View view) {
        String password = preferences.getString("password", "");
        Log.e("TAG", "get data from sp password: "+password);
        String decrypt = encryptUtils.aesDecrypt(password, 1);
        Log.e("TAG", "decrypt password: "+decrypt);
    }

    private void initGCM() {
        encryptUtils = new EncryptUtils();
        encryptUtils.initGCM();
    }
}

